/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cadProdutoJInternalFrame.java
 *
 * Created on 27/10/2009, 21:03:56
 */
package br.com.fatecpg.zanotti.jframes.cadastros;

import br.com.fatecpg.zanotti.acessobanco.conTableFornecedor;
import br.com.fatecpg.zanotti.acessobanco.dmlFornecedor;
import br.com.fatecpg.zanotti.jframes.consultas.conFornecJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Fornecedor;

/**
 *
 * @author Luiz Zanotti
 */
public class cadFornecJInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form cadProdutoJInternalFrame */
    public cadFornecJInternalFrame() {
        initComponents();

        atualizaCodigoPre();


    }
    private static cadFornecJInternalFrame p;

    public static cadFornecJInternalFrame getInstancia() {
        if (p == null) {
            p = new cadFornecJInternalFrame();
        }
        return p;
    }

    private void atualizaCodigoPre() {
        codjTextField.setText(conTableFornecedor.ConsultaCodigo());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        codjTextField = new javax.swing.JTextField();
        cnpjjFormattedTextField = new javax.swing.JFormattedTextField();
        nomejTextField = new javax.swing.JTextField();
        emailjTextField = new javax.swing.JTextField();
        telefonejFormattedTextField = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        endjTextArea = new javax.swing.JTextArea();
        cadastrarjButton = new javax.swing.JButton();
        selectExistjButton = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Cadastro de Fornecedores");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("Código:");

        jLabel2.setText("CNPJ:");

        jLabel3.setText("Nome:");

        jLabel4.setText("Endereço:");

        jLabel5.setText("E-mail:");

        jLabel6.setText("Telefone:");

        codjTextField.setEnabled(false);

        try {
            telefonejFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        telefonejFormattedTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                telefonejFormattedTextFieldActionPerformed(evt);
            }
        });

        endjTextArea.setColumns(15);
        endjTextArea.setLineWrap(true);
        endjTextArea.setRows(5);
        jScrollPane1.setViewportView(endjTextArea);

        cadastrarjButton.setText("Cadastrar");
        cadastrarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadastrarjButtonActionPerformed(evt);
            }
        });

        selectExistjButton.setText("Selecionar Existente");
        selectExistjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectExistjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(codjTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .addComponent(cnpjjFormattedTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .addComponent(nomejTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .addComponent(telefonejFormattedTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .addComponent(emailjTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)))
                    .addComponent(cadastrarjButton)
                    .addComponent(selectExistjButton))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(selectExistjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cnpjjFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nomejTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(telefonejFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cadastrarjButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void telefonejFormattedTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_telefonejFormattedTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_telefonejFormattedTextFieldActionPerformed

    private void cadastrarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastrarjButtonActionPerformed
        Fornecedor Fornecedor = new Fornecedor();

        Fornecedor.setCDCNPJFORNECEDOR(cnpjjFormattedTextField.getText());
            Fornecedor.setCDFORNECEDOR(codjTextField.getText());
        Fornecedor.setCDTELEFONE(telefonejFormattedTextField.getText().replace("(", "").replace(")", "").replace("-", ""));
        Fornecedor.setNMEMAIL(emailjTextField.getText());
        Fornecedor.setNMENDERECO(endjTextArea.getText());
        Fornecedor.setNMFORNECEDOR(nomejTextField.getText());


        boolean b;

        if (cadastrarjButton.getText().equals("Cadastrar")) {
            b = dmlFornecedor.cadastrar(Fornecedor);
        } else {

            b = dmlFornecedor.atualizar(Fornecedor);
        }


        this.setTitle("Cadastro de Cliente");
        cadastrarjButton.setText("Cadastrar");

        if (b) {
limpaTudo();

        }




    }//GEN-LAST:event_cadastrarjButtonActionPerformed
public void limpaTudo(){
            endjTextArea.setText("");
            emailjTextField.setText("");
            cnpjjFormattedTextField.setText("");
            nomejTextField.setText("");
            telefonejFormattedTextField.setText("");
            atualizaCodigoPre();
}
    private void selectExistjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectExistjButtonActionPerformed
        this.setVisible(false);
        conFornecJInternalFrame.getInstancia().setVisible(true);
    }//GEN-LAST:event_selectExistjButtonActionPerformed

    public void setCadastrarjButton(String txt) {
        cadastrarjButton.setText(txt);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cadastrarjButton;
    private javax.swing.JFormattedTextField cnpjjFormattedTextField;
    private javax.swing.JTextField codjTextField;
    private javax.swing.JTextField emailjTextField;
    private javax.swing.JTextArea endjTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nomejTextField;
    private javax.swing.JButton selectExistjButton;
    private javax.swing.JFormattedTextField telefonejFormattedTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * @param cnpjjFormattedTextField the cnpjjFormattedTextField to set
     */
    public void setCnpjjFormattedTextField(String txt) {
        this.cnpjjFormattedTextField.setText(txt);
    }

    /**
     * @param codjTextField the codjTextField to set
     */
    public void setCodjTextField(String txt) {
        this.codjTextField.setText(txt);
    }

    /**
     * @param emailjTextField the emailjTextField to set
     */
    public void setEmailjTextField(String txt) {
        this.emailjTextField.setText(txt);
    }

    /**
     * @param endjTextArea the endjTextArea to set
     */
    public void setEndjTextArea(String txt) {
        this.endjTextArea.setText(txt);
    }

    /**
     * @param nomejTextField the nomejTextField to set
     */
    public void setNomejTextField(String txt) {
        this.nomejTextField.setText(txt);
    }

    /**
     * @param telefonejFormattedTextField the telefonejFormattedTextField to set
     */
    public void setTelefonejFormattedTextField(String txt) {
        this.telefonejFormattedTextField.setText(txt);
    }
}
